<!-- 在 'manifest.json' 中允许打开摄像头权限 -->

<template>
	<view @click="scanQRcodes" class="iconfont icon-saoma text-green icon"></view>
</template>

<script>
import { showConfirm} from '@/utils/common'
import {scanCodeLoginConfirm} from '@/api/login.js'
	export default {
		data() {
			return {
			}
		},

		methods: {
			scanQRcodes() {
				uni.scanCode({
					success: (res) => {
						console.log(res)
						if (res.result) {
							let token = res.result
							showConfirm('登录').then(async res => {
							  if (res.confirm) {
								await scanCodeLoginConfirm(token || 'deu')
							  }
							})
							
							console.log('扫描结果：', res.result);
						} else {
							uni.showToast({
								title: '扫描失败',
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						if (err.errMsg.includes('cancel')) {
							uni.showToast({
								title: '扫描已取消',
								icon: 'none'
							});
						} else {
							uni.showToast({
								title: '调用相机失败',
								icon: 'none'
							});
						}
					}
				});
			}
		}
	}
</script>